package interceptor;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LogInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println(request.getRemoteAddr()+"访问了"+handler);

        System.out.println("handler.getClass() = " + handler.getClass());

        HandlerMethod handlerMethod= (HandlerMethod) handler;

        System.out.println("handlerMethod.getBean() = " + handlerMethod.getBean());
        System.out.println("handlerMethod.getBeanType() = " + handlerMethod.getBeanType());
        System.out.println("handlerMethod.getMethod() = " + handlerMethod.getMethod());
        System.out.println("handlerMethod.getMethodParameters() = " + handlerMethod.getMethodParameters());
        System.out.println("handlerMethod.getReturnType() = " + handlerMethod.getReturnType());
        System.out.println("handlerMethod.getMethodAnnotation(RequestMapping.class) = " + handlerMethod.getMethodAnnotation(RequestMapping.class));
        
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println(handler+"执行结束");
        System.out.println(modelAndView.getViewName());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("即将生成响应给客户端");
    }
}
